const nextGreaterElement = (nums1, nums2) => {
  const ans = [];
  const stack = [];
  const map = new Map();
  for (let i = 0; i < nums2.length; i++) {
    while (stack.length && nums2[i] > stack[stack.length - 1]) {
      map.set(stack.pop(), nums2[i]);
    }
    stack.push(nums2[i]);
  }
  while (stack.length) {
    map.set(stack.pop(), -1);
  }
  nums1.forEach((num) => {
    ans.push(map.get(num));
  });
  return ans;
};
